package com.anywhere.freightman.order.controller;

import com.anywhere.freightman.common.ContextKeys;
import com.anywhere.freightman.common.annotation.Permission;
import com.anywhere.freightman.common.entity.order.AddressBookQueryEntity;
import com.anywhere.freightman.common.entity.user.Account;
import com.anywhere.freightman.common.request.PageRequest;
import com.anywhere.freightman.order.controller.request.AddressBookRequest;
import com.anywhere.freightman.order.model.AddressBookModel;
import com.anywhere.freightman.order.service.IAddressBookService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("address/book")
public class AddressBookController {

    @Autowired
    private IAddressBookService addressBookService;

    @Permission(code = "COMMON")
    @GetMapping("page")
    @ApiOperation("分页")
    public PageInfo<AddressBookModel> getPageList(HttpServletRequest httpRequest,
                                                  AddressBookRequest qm) {
        Account account = (Account) httpRequest.getAttribute(ContextKeys.REQUEST_ACCOUNT);
        qm.setCompanyId(account.getCompanyId());
        PageRequest<AddressBookQueryEntity> pageRequest = new PageRequest<>();
        BeanUtils.copyProperties(qm, pageRequest);
        AddressBookQueryEntity entity = new AddressBookQueryEntity();
        BeanUtils.copyProperties(qm, entity);
        pageRequest.setEntity(entity);
        PageInfo<AddressBookModel> pageList = addressBookService.getPageList(pageRequest);
        PageInfo<AddressBookModel> resp = new PageInfo<>();
        BeanUtils.copyProperties(pageList, resp);
        return resp;
    }
}
